package com.ruoyi.demo.mycontroller.login;

import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.CodePam;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.user.UserException;
import com.ruoyi.demo.myservice.MyLoginService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.SysLoginService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/login")
public class ToThirdPartController extends BaseController{

    //登录自定义服务
    @Autowired
    private MyLoginService myLoginService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysLoginService loginService;

    /**
     * @Description: 平台带着token来系统里面登陆
     * 这边需要做两个步骤：
     * 1.检测数据库里面有没有这个用户名，有则不操作，无则添加
     * 2.去平台验证一下Token是否有，有的话继续操作后面的登录
     * 平台没有这个token，则直接打回去，不让上来
     * 第一步：获取用户id
     * 第二步：查询用户是否存在
     *      不存在：返回错误
     *  第三步：构造用户对象  创建token  返回用户信息
     * @author: fjw
     * @date: 2022/4/19 上午 11:38
     * @Return: com.ruoyi.common.core.domain.AjaxResult
     */
    //,@RequestHeader("Authorization") String authorization
    @SaIgnore
    @PostMapping("/ssologin")
    public AjaxResult toThirdPartGetAuthJHaveToken(@RequestBody CodePam  pam) throws IOException {
        String code = pam.getCode();
        String token = myLoginService.getToken(code);
        //返回结果
        AjaxResult ajax = null;
        //调用验证token的方法，就是去服务器上验证token是否有效
        String  userId = myLoginService.checkJToken(token);
        if("".equals(userId) || userId==null){
            ajax = AjaxResult.error("认证错误");
            return ajax;
        }
        //查询用户是否存在
        SysUser user = userService.selectUserById(Long.parseLong(userId));
        if (user==null){
            //用户不存在时处理逻辑
            System.out.println("用户不存在。");
            ajax = AjaxResult.error("用户不存在,登录失败。");
            return ajax;
        }
        ajax = AjaxResult.success();
        String password = user.getPassword();

//        // 生成令牌(不加验证码登录)
        String tokenNew = loginService.loginNoCode(user.getUserName(),password, null);

        System.out.println("token:"+tokenNew);
        ajax.put(Constants.TOKEN, tokenNew);
        ajax.put("msg", "登录成功");
        return ajax;
    }

}
